<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>JavaScripTools Manual</title>
    <link rel="stylesheet" type="text/css" href="../../css/style.css">
</head>

<body>
<br>
<h1>JavaScripTools Manual</h1>
<h2>Parsers</h2>
<h3>Using the CustomParser</h3>
<p>The 
<a href="../api/class_CustomParser.html">CustomParser</a>
uses a custom function to format and another to parse the data.</p>
<p>It receives 2 arguments: formatFunction and parseFunction. They are both functions
that take 1 argument (the value to be formatted or parsed), and are called
on the parser's format and parse methods, and defaults to a function that returns
the received value.</p>
<p>Examples:</p>
<pre>//A StringTrimmer parser using the JavaScriptUtil trim function
var parser = new CustomParser(null, trim);
parser.format(" ABC ") -> "ABC"
parser.parse(" ABC ") -> " ABC "

//A percentual parser using custom functions
var parser2 = new CustomParser();
parser2.formatFunction = function (value) {
    try {
        return round(value * 100) + "%";
    } catch (exception) {
        return null;
    }
};
parser2.parseFunction = function (value) {
    try {
        var number = replaceAll(value, "%", "");
        return isEmpty(number) ? null : parseInt(number, 10) / 100;
    } catch (exception) {
        return null;
    }
};
parser.format(0.1) -> "10%"
parser.format(1) -> "100%"
parser.parse("95%") -> 0.95
</pre>

<br><br>
<hr>
<table width="100%">
    <tr>
        <td width="33%" align="left"><b>Previous:</b><br><a href="Parsers_date.html">Working with the DateParser</a></td>
        <td width="34%" align="center"><a href="index.html">Table of Contents</a></td>
        <td width="33%" align="right"><b>Next:</b><br><a href="Parsers_wrapper.html">Using the WrapperParser</a></td>
    </tr>
</table>

</body>
</html>
